package cn.zhp.controller;

import cn.zhp.entity.Result;
import cn.zhp.entity.SysResource;
import cn.zhp.entity.SysUser;
import cn.zhp.service.SysResourceService;
import cn.zhp.service.SysUserService;
import cn.zhp.util.EncryptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 渲染类
 * */
@Controller
public class MainController {

    @Autowired
    SysUserService userService;

    @Autowired
    SysResourceService resourceService;

    /**
     * 统一页面头返回
     * */
    @RequestMapping("navbar")
    public String navbar(){
        return "/common/navbar";
    }
    /**
     * 统一侧边栏页面返回
     * */
    @RequestMapping("sidebar")
    public String sidebar(){
        return "/common/sidebar";
    }



    /**
     * 登录接口
     * */
    @RequestMapping("doLogin")
    @ResponseBody
    public Result doLogin(@RequestBody Map<String,String> params, HttpSession session){
        /**
         * 1.验证验证码是否正确  正确继续    错误则返回登录错误
         * 2.验证账号密码，正确则返回正确信息，客户端跳转到index页   错误则返回错误信息
         */
        //判断是否有传入code码（验证码）/或则判断是否为空
        if (params.containsKey("vcode")&& !StringUtils.isEmpty(params.get("vcode"))) {
            String vcode =  params.get("vcode");
            //判断验证码是否与服务器生成的验证码一致
            if (vcode.equals(session.getAttribute("checkCode"))){
                //判断账户与密码
                if(params.containsKey("name")&& !StringUtils.isEmpty(params.get("name"))){
                    if(params.containsKey("password")&& !StringUtils.isEmpty(params.get("password"))){
                        SysUser sysUser = new SysUser();
                        String name = params.get("name");
                        String password = params.get("password");
                        sysUser.setUsername(name);
                        sysUser.setPassword(EncryptUtils.MD5_HEX(EncryptUtils.MD5_HEX(password)+name));
                        SysUser loginUser = userService.selectOne(sysUser);
                        if(loginUser!=null){//正确登录
                            //保持用户登录状态
                            session.setAttribute("loginUser",loginUser);
                            //密码设为null
                            loginUser.setPassword(null);
                            //侧边栏，权限授权，获取用户所有可访问资源(菜单)  保持状态
                            List<SysResource> resources = resourceService.selectByUid(loginUser.getId());
                            //session保存用户能访问那些权限
                            session.setAttribute("resources",resources);
                            HashMap<String, Object> map = new HashMap<>();
                            map.put("loginUser",loginUser);
                            map.put("resources",resources);
                            return new Result(true,"登录成功",map);
                        }else{
                            return new Result(false,"用户名或密码错误",null);
                        }
                    }
                }

            }else{
                return new Result(false,"验证码错误",null);
            }
        }
        return new Result(false,"登录失败，请重试",null);
    }

    /**
     * 用户登出
     * */
    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/login.html";  //重定向不再走视图解析器
    }

    /**
     * 登录后的初始页面
     * */
    @RequestMapping("toIndex")
    public String toIndex(){
        return "/index";
    }

}
